package com.zhang.dao.impl;

import com.zhang.dao.UserDao;
import com.zhang.domain.User;
import com.zhang.utils.JdbcUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate template = new JdbcTemplate(JdbcUtils.getDataSource());

    @Override
    public void save(User user) {
        String sql = "INSERT INTO `userpro`.`user` (`id`,`username`,`password`,`email`,`sex`) VALUES(?,?,?,?,?)";
        template.update(sql,null,user.getUsername(),user.getPassword(),user.getEmail(),user.getSex());
    }

    @Override
    public void saveUserLove(int userId, int loveId) {
        String sql = "INSERT INTO `userpro`.`user_love` (`userID`, `loveId`) VALUES (?, ?)";
        template.update(sql,userId,loveId);
    }

    @Override
    public User login(String username, String password) {
        String sql = "SELECT * FROM `userpro`.`user` WHERE username = ? AND PASSWORD = ?";
        try {
            return  template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
        } catch (DataAccessException e) {
            return null;
        }

    }





    @Override
    public Integer getTotal(Map<String, Object> condition) {
        //1.创建查询sql + 查询参数
        String sql = "SELECT COUNT(*) FROM `user`  WHERE 1=1   ";
        StringBuilder sb = new StringBuilder(sql);
        ArrayList<Object> params = new ArrayList<>();

        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if(null != key && !"".equals(key)){
                sb.append("AND "+ key +" LIKE ? ");
                params.add("%"+condition.get(key)+"%");
            }
        }
        sql = sb.toString();


        //2.查询
        return template.queryForObject(sql,Integer.class,params.toArray());
    }

    @Override
    public List<User> Page(Map<String, Object> condition, int currentPage, int row) {
        //1.创建查询sql + 查询参数
        String sql = "SELECT * FROM `user`  WHERE 1=1    ";
        StringBuilder sb = new StringBuilder(sql);
        ArrayList<Object> params = new ArrayList<>();

        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if(null != key && !"".equals(key)){
                sb.append("AND "+ key +" LIKE ? ");
                params.add("%"+condition.get(key)+"%");
            }
        }

        sb.append(" limit ? , ? ");
        params.add(currentPage);
        params.add(row);
        sql = sb.toString();

        //2.查询
        return template.query(sql,new BeanPropertyRowMapper<User>(User.class),params.toArray());
    }




    @Override
    public void delUser(int id) {
        String sql = "DELETE FROM `userpro`.`user` WHERE `id` = ?";
        template.update(sql,id);
    }

    @Override
    public void delUserLove(int userId) {
        String sql = "DELETE FROM `userpro`.`user_love` WHERE `userID` = ?";
        template.update(sql,userId);
    }

    @Override
    public User select(int id) {
        String sql = "SELECT * FROM `userpro`.`user` WHERE id = ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),id);
    }

    @Override
    public void update(User user) {
        String sql = "UPDATE  `user` SET `username` = ? , `password` = ? , `email` = ?, `sex` = ? WHERE `id` = ?";
        template.update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getSex(),user.getId());
    }

    @Override
    public void delLove(int userId) {
        String sql = "DELETE FROM `userpro`.`user_love` WHERE `userID` = ?";
        template.update(sql,userId);
    }


}
